package com.wnhz.work.yz.activity;

import android.app.Activity;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.wnhz.work.yz.BaseActivity;
import com.wnhz.work.yz.BuildConfig;
import com.wnhz.work.yz.MainActivity;
import com.wnhz.work.yz.MyApplication;
import com.wnhz.work.yz.R;
import com.wnhz.work.yz.utils.DataCleanManager;
import com.wnhz.work.yz.utils.Url;
import com.wnhz.work.yz.utils.xutils3.MyCallBack;
import com.wnhz.work.yz.utils.xutils3.XUtil;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

/**
 * ----设置
 */
public class SettingActivity extends BaseActivity implements View.OnClickListener {

    private TextView tv_setting_phone;
    private TextView tv_setting_cache;
    private TextView tv_setting_version;
    private Activity activity;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_setting);

        initTitle();
        initView();
    }

    private TextView title;

    private void initTitle() {
        title = (TextView) findViewById(R.id.view_main_middle_title);
        title.setText("设置");
        findViewById(R.id.btn_main_left).setOnClickListener(this);
    }

    private void initView() {
        tv_setting_phone = (TextView) findViewById(R.id.tv_setting_phone);
        tv_setting_cache = (TextView) findViewById(R.id.tv_setting_cache);
        tv_setting_version = (TextView) findViewById(R.id.tv_setting_version);

        if (MyApplication.getInstance().getUserBean() != null) {
            tv_setting_phone.setText(MyApplication.getInstance().getUserBean().getPhone());
        }
//        tv_setting_cache.setText("13.24M");
        setDataCleanManager();
        tv_setting_version.setText(BuildConfig.VERSION_NAME + "");

        findViewById(R.id.rl_setting_phone).setOnClickListener(this);
        findViewById(R.id.rl_setting_pwd).setOnClickListener(this);
        findViewById(R.id.rl_setting_cache).setOnClickListener(this);
        findViewById(R.id.rl_setting_about).setOnClickListener(this);
        findViewById(R.id.tv_setting_exit).setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        Intent intent;
        switch (v.getId()) {
            case R.id.btn_main_left:
                finish();
                break;

            //手机号码修改
            case R.id.rl_setting_phone:
                intent = new Intent(this, VerifyPhoneActivity.class);
                intent.putExtra("title", "手机");
                startActivity(intent);
                break;

            //密码
            case R.id.rl_setting_pwd:
                intent = new Intent(this, VerifyPhoneActivity.class);
                intent.putExtra("title", "密码");
                startActivity(intent);
                break;

            //清除本地缓存
            case R.id.rl_setting_cache:
//                MyToast("清除本地缓存");
                cleanDialog();
                break;

            //关于
            case R.id.rl_setting_about:
                // MyToast("关于");
                break;

            //退出登录
            case R.id.tv_setting_exit:
                upload();
                break;

            default:
                break;
        }

    }

    private static final String TAG = "SettingActivity";
    private String message;

    private void upload() {
        showDialog();
        Map<String, Object> map = new HashMap<>();
        map.put("token", MyApplication.getInstance().getUserBean().getToken());
        XUtil.Post(Url.MEMBER_LOGOUT, map, new MyCallBack<String>() {
            @Override
            public void onSuccess(String result) {
                // closeDialog();
                super.onSuccess(result);
                Log.i(TAG, "onSuccess: out= " + result);
                try {
                    JSONObject jsonObject = new JSONObject(result);
                    String str = jsonObject.getString("result");
                    message = jsonObject.optString("message");
                    if ("1".equals(str)) {
                        SharedPreferences sp = getSharedPreferences("login", 0);
                        sp.edit().clear().commit();
                        message = "退出成功";
                        MyApplication.getInstance().userBean = null;

                        MyApplication.getInstance().gotoLoginActivity();
                    } else {
                            SharedPreferences sp = getSharedPreferences("login", 0);
                            sp.edit().clear().commit();
                            message = "退出成功";
                            MyApplication.getInstance().userBean = null;
                            MyApplication.getInstance().gotoLoginActivity();
                        Toast.makeText(SettingActivity.this, "" + message, Toast.LENGTH_SHORT).show();
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFinished() {
                closeDialog();
                if ("退出成功".equals(message))
                    Toast.makeText(SettingActivity.this, "" + message, Toast.LENGTH_SHORT).show();
                super.onFinished();
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                closeDialog();
                Toast.makeText(SettingActivity.this, "" + message, Toast.LENGTH_SHORT).show();
                super.onError(ex, isOnCallback);
            }
        });
    }

    /**
     * ----弹窗确认是否清除缓存
     */
    private void cleanDialog() {
        android.support.v7.app.AlertDialog.Builder builder = new android.support.v7.app.AlertDialog.Builder(this);
        builder.setMessage("是否清除缓存");
        builder.setNegativeButton("取消", null);
        builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                DataCleanManager.clearAllCache(SettingActivity.this);
                setDataCleanManager();
            }
        });
        builder.show();
    }

    /**
     * ----清除缓存
     */
    private void setDataCleanManager() {
        try {
            tv_setting_cache.setText(DataCleanManager.getTotalCacheSize(getApplicationContext()));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
